<%@page import="cn.qidisoft.edu.hzjt.model.Student"%>
<%@ page language="java" pageEncoding="UTF-8" buffer="16kb"%>
<%@ include file="/base.jsp"%>
<%@ include file="/module/taglibs.jsp"%>
<%@ include file="/module/nocache.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<%@ include file="/module/js.jsp"%>
<%@ include file="/module/orderField.jsp"%>
<script type="text/javascript" src="${basePath}/js/jquery.Huploadify.js"></script>
<link href="${basePath}/css/Huploadify.css" rel="stylesheet">
<style>
table label.error {
	margin-left: 5px;
	width: auto;
	display: inline;
	color: red;
}
</style>
<style type="text/css">
.grid {
	font: 12px arial, helvetica, sans-serif;
	border: 1px solid #cccccc
}

.grid td {
	font: 100% arial, helvetica, sans-serif;
	height: 24px;
	padding: 5px
}

.grid {
	width: 100%;
	border-collapse: collapse
}

.grid th {
	background: #eeeeee;
	height: 27px;
	line-height: 27px;
	border: 1px solid #cccccc;
	padding-left: 5px
}

.grid td {
	border: 1px solid #cccccc;
	padding-left: 5px;
	text-align: center;
}
</style>
</head>
<body>
	<!-- 业余党校开展情况表 -->
	<form action="#" name="Form" id="Form" method="post">
		<input type="hidden" name="flag" id="flag" value="" />
		<input type="hidden" name="dxid" id="dxid" value="${das.dxid}" />
		<table cellpadding="0" cellspacing="0" border="0" class="table-border"
			width="100%">
			<tr>
				<td class="table-header" colspan="4">业余党校开展情况</td>
			</tr>
			<tr>
				<td class="table-titleTD" style="width: 15%"><font
					style="color: red;">*</font>党校期数:</td>
				<td class="table-contentTD" style="width: 35%"><input type="text" id="dxqs" value="${das.dxqs}"
					name="dxqs" autocomplete="off" maxlength="100" style="width: 70%"
					class="required inputText" /></td>

				<td class="table-titleTD" style="width: 15%"><font
					style="color: red;">*</font>授课主题:</td>
				<td class="table-contentTD" style="width: 35%"><input type="text" name="skzt" style="width: 70%"
					id="skzt" autocomplete="off" class="required inputText" value="${das.skzt}"/></td>
			</tr>
			<tr>
				<td class="table-titleTD" style="width: 15%"><font
					style="color: red;">*</font>授课时间:</td>
				<td class="table-contentTD" style="width: 35%"><input type="text" id="sksj"
					name="sksj" autocomplete="off" maxlength="100" value="${das.sksj}" style="width: 70%"
				  class="Wdate required text" onFocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})"
				  style="width: 70%" /></td>

				<td class="table-titleTD" style="width: 15%"><font
					style="color: red;">*</font>授课地点:</td>
				<td class="table-contentTD" style="width: 35%"><input type="text" name="skdd" value="${das.skdd}"
					id="skdd" autocomplete="off" class="required inputText" style="width: 70%"/></td>
			</tr>
			<tr>
				<td class="table-titleTD" style="width: 15%">计划参与人数:</td>
				<td class="table-contentTD" style="width: 35%"><input type="number" id="jhcyrs" value="${das.jhcyrs}"
					name="jhcyrs" autocomplete="off" maxlength="100" class="inputText" /></td>

				<td class="table-titleTD" style="width: 15%"><font
					style="color: red;">*</font>实际授课人数:</td>
				<td class="table-contentTD" style="width: 35%"><input type="number" name="sjskrs" value="${das.sjskrs}"
					id="sjskrs" autocomplete="off" class="required inputText" style="width: 70%"/></td>
			</tr>
			<tr>
				<td class="table-titleTD" style="width: 15%">创建人:</td>
				<td class="table-contentTD" style="width: 35%">${das.creatorName}</td>
			</tr>
			<tr>
				<td class="table-titleTD" style="width: 15%">附件：</td>
				<td class="table-contentTD" colspan="3" style="width: auto;">
					<table>
						<tr>
							<td colspan="2">
								<div id="uploadadd"></div>
							</td>
						</tr>
						<c:if test="${fn:length(dataList) > 0}">
							<c:forEach items="${dataList}" var="datalist" varStatus="index">
								<tr>
									<td>
										[文件] ${datalist.fileName}
									</td>
									<td>
										<a href="${basePath}/DownLoadFileServlet?filename=${datalist.fileName}&filepath=${datalist.filePath}">下载</a>
										
										<%-- <button type="button"  onclick="window.location.href='${basePath}/DownLoadFileServlet?filename=${datalist.fileName}&filepath=${datalist.filePath}'">下载</button> --%>
										&nbsp;&nbsp;
										<button id="${datalist.fileId}_hr" type="button"  onclick="deleteByPK('${datalist.fileId}','${datalist.filePath}');">删除</button>
										<%-- <button id="${datalist.gid}_hr" type="button"  onclick="deleteByPK('${datalist.gid}');">删除</button> --%>
									</td>
								</tr>
							</c:forEach>
						</c:if>
					</table>
				</td>
			</tr>
			<tr>
				<td class="table-titleTD" style="width: 15%">备注:</td>
				<td class="table-contentTD" colspan="3" style="width: auto;"><textarea rows="8"
						cols="150" name="remark" id="remark" maxlength="2000">${das.remark}</textarea></td>
			</tr>
			<tr>
				<td class="table-titleTD" >参与人员</td>
				<td class="table-contentTD" colspan="3"><input type="button"
					id="optionBtn" value="选择" class="inputButton"
					onclick="javascript:showdiv();" placeholder="请选择学生" /> <input
					type="button" id="delSelect" value="删除" class="inputButton" /></td>
			</tr>
			<tr>
				<td colspan="4">
					<table class="grid">
						<thead>
							<tr>
								<th><input type="checkbox" name="selectall" id="selectall" /></th>
								<th>序号</th>
								<th>姓名</th>
								<th>性别</th>
								<th>学号</th>
								<th>班级</th>
								<th>专业</th>
								<th>年级</th>
							</tr>
						</thead>
						<tbody id="stuTbody">

						</tbody>
					</table>
				</td>
			</tr>
		</table>
	</form>
	<!-- 添加到学生表格stuTbody的内容 -->
	<script id="tpl_dy" type="text/html">
        <tr id="tr_{{xsid}}">
            <td>&nbsp;<input type="checkbox" name="dyItems" value="{{xsid}}"/></td>
            <td>{{iindex+1}}</td>
            <td>{{xm}}</td>
            <td>{{xb}}</td>
            <td>{{xh}}</td>
            <td>{{bjmc}}</td>
			<td>{{zymc}}</td>
            <td>
				{{njmc}}
				<input type="hidden" name="deveAmateurSchoolStudentsList[{{iindex}}].xsid" value="{{xsid}}"/>
				<input type="hidden" name="deveAmateurSchoolStudentsList[{{iindex}}].bjid" value="{{bjid}}"/>
			</td>
        </tr>
    </script>
	<!-- 弹出学生DIV -->
	<div id="div1" style="display: none;">
		<table cellpadding="0" cellspacing="0" border="0" class="table-border">
			<tr>
				<td style="clear: both; width: 45%"><input class="text"
					name="searchStu"
					ondblclick="javascript:filterSelectItems('sourseFromSelect2','fromSelect2','toSelect2',this.value);"
					onkeyup="javascript:filterSelectItems('sourseFromSelect2','fromSelect2','toSelect2',this.value);" />
					<font color="green">在此查找</font> <br />
				<br /> <label style="color: red;">未选中:</label><br /> <select
					name="fromSelect2" id="fromSelect2" multiple="multiple"
					style="width: 400px; height: 300px;"
					ondblclick="moveField('fromSelect2','toSelect2');">
						<c:forEach items="${students}" var="student">
							<option value="${student.xsid}">${student.xm}</option>
						</c:forEach>
				</select> <select name="sourseFromSelect2" id="sourseFromSelect2"
					multiple="multiple"
					style="width: 400px; height: 300px; display: none;"
					ondblclick="moveField('fromSelect2','toSelect2');">
						<c:forEach items="${students}" var="student">
							<option value="${student.xsid}">${student.xm}</option>
						</c:forEach>
				</select></td>
				<td align="center" style="width: 10%"><input type="button"
					onclick="moveAllField('fromSelect2','toSelect2');"
					style="width: 50px;" value=">>" /> <br />
				<br /> <input type="button"
					onclick="moveField('fromSelect2','toSelect2');"
					style="width: 50px;" value=">" /> <br />
				<br /> <input type="button" style="width: 50px;" value="&lt"
					onclick="moveField('toSelect2','fromSelect2');" /><br /> <br /> <input
					type="button" style="width: 50px;"
					onclick="moveAllField('toSelect2','fromSelect2');" value="&lt&lt" />
				</td>
				<td style="width: 45%"><br />
				<label style="color: red;">已选中:</label><br /> <select
					name="toSelect2" id="toSelect2" multiple="multiple" class="text"
					style="width: 400px; height: 300px;"
					ondblclick="moveField('toSelect2','fromSelect2');"></select></td>
			</tr>
		</table>
	</div>

	<!-- 操作表格 -->
	<br />
	<table cellpadding="1" cellspacing="1" border="0" align="left">
		<c:if test="${_login_user_type==2}">
		<tr>
			<td>
			<kl:permission permisstionList="${permisstionList}"
               currentUser="${_login_console_user}"
               currentCatalogId="<%=CatalogUtils.getCatalogId(\"ZHDJ_YYDX_DXGL\")%>" key="modify">
			<input type="button" id="btnSave" class="inputButton"
				style="text-align: center;" value="保存" />
			</kl:permission>
			</td>
			<td>
			<kl:permission permisstionList="${permisstionList}"
               currentUser="${_login_console_user}"
               currentCatalogId="<%=CatalogUtils.getCatalogId(\"ZHDJ_YYDX_DXGL\")%>" key="modify">
			<input type="button" id="btnSaveContinue" class="inputButton"
				style="text-align: center;" value="保存并新增" />
			</kl:permission>
			</td>
			<td><input type="button" id="Cancel" class="inputButton"
				style="text-align: center;" value="取消" /></td>
			<td><input type="button" id="returnBut" class="inputButton"
				value="返回" style="text-align: center;" /></td>
		</tr>
	  </c:if>	
	</table>

	<script>
		var html;
		//点击弹出添加学生div
		function showdiv() {
			$("#toSelect2").empty();
			$('#div1').dialog('open');
		}
		
		$(document).ready(function() {
			//返回方法
			function backList(){
				window.location.href = "${basePath}/deveAmateurSchool/list";
			}
			
			//返回
			$("#returnBut").click(function(){
				backList();
			});
			
			//保存
			$('#btnSave').click(function(){
				var _form=$("#Form").validate();
				if(_form.form()){
					document.Form.action ="${basePath}/deveAmateurSchool/update?action=1";
					document.Form.submit();
				}
			});
			
			// 点击保存并新增
			$("#btnSaveContinue").click(function(){
				var _form=$("#Form").validate();
				if(_form.form()){
					document.Form.action ="${basePath}/deveAmateurSchool/update?action=2";
					document.Form.submit();
				}
			});
			
			//取消
			$("#Cancel").click(function() {
				window.location.href = "${basePath}/deveAmateurSchool/preadd";
		 	});

			var json_dy_info = JSON.parse('${json}');
			var data_canjia = JSON.parse('${deveAmateurSchoolStudentsJSON}');
			 if(data_canjia!=null && data_canjia.length>0){
				 drawTable("tpl_dy","stuTbody",data_canjia);
	         }
			//点击添加学生
			$("#div1").dialog({
				width : 'auto',
				height : 'auto',
				bgiframe : true,
				modal : true,
				draggable : true,
				resizable : true,
				title : '选择学生',
				autoOpen : false,
			    buttons : {
			    	'取消' : function() {
			    		$(this).dialog('close');
					},
					'确定' : function() {
						var data = [];
						var toItems = document.getElementById('toSelect2');
						if(toItems.options.length==0){
							alert("请选择一项");
							return false;
					 	}else{
					 		for(var i =0;i<toItems.options.length;i++){
					 			var stu_id = toItems.options[i].value;
					 			for(var j=0;j<json_dy_info.length;j++){
                                    if(stu_id==json_dy_info[j].xsid){
                                        data.push({//往对象的末尾添加一个对象
                                        	xsid:json_dy_info[j].xsid,
                                            xm: json_dy_info[j].xm,
                                            xb: json_dy_info[j].xb,
                                            xh: json_dy_info[j].xh,
                                            bjmc: json_dy_info[j].bjmc,
                                            zymc: json_dy_info[j].zymc,
                                            njmc: json_dy_info[j].njmc,
                                            bjid: json_dy_info[j].bjid
                                        });
                                    }
                                }
					 		}
					 		//初始化表格数据
						 	data_canjia = getTargetJSON(data,data_canjia);
	                        drawTable("tpl_dy","stuTbody",data_canjia);
					 	}
						$(this).dialog('close');
					}
				}
			});
			
			$("#delSelect").click(function(){
                var iii = $.getSelectIds('dyItems');
                if(iii.length==0){
                    alert('请至少选择一项删除');
                    return false;
                }
                $("input:checkbox[name='dyItems']:checked").each(function() {
                    removeObjFromJSON($(this).val(),data_canjia);
                });
                drawTable("tpl_dy","stuTbody",data_canjia);
            });
			$.select2("selectall","dyItems");
			
			//判断是否已经存在该元素值
	        function getTargetJSON(fromJSON,toJSON){
	            if(toJSON==null || toJSON.length==0){
	                toJSON = fromJSON;
	            }else{
	                for(var i=0;i<fromJSON.length;i++){
	                    var xsid = fromJSON[i].xsid;
	                    var isExist = 0;
	                    for(var j=0;j<toJSON.length;j++){
	                        if(xsid==toJSON[j].xsid){
	                            isExist=1;
	                        }
	                    }
	                    if(isExist==0){
	                        toJSON.push(fromJSON[i]);
	                    }
	                }
	            }
	            return toJSON;
	        }
			
			
			//输出表格数据
	        function drawTable(tplId,table_id,data) {
	            $("#"+table_id).html('');
	            for(var k=0;k<data.length;k++){
	                data[k].iindex = k;
	                var html = template(tplId,data[k]);
	                $("#"+table_id).html($("#"+table_id).html()+html);
	            }
	        }
			
	      //从JSON中移除某个对象
	        function removeObjFromJSON(xsid,targetJSON) {
	            for(var i=0;i<targetJSON.length;i++){
	                if(targetJSON[i].xsid == xsid) {
	                    targetJSON.splice(i,1);
	                }
	            }
	        }
			
			
		});//$(document).ready end
		
		
		//图片上传
		var file_path = "prefix";
		var file_path_one = null;
		var fileMap = new Array();
		$(function(){
			var up = $('#uploadadd').Huploadify({
					auto:false,
					fileTypeExts:'*.*',
					multi:true,
					formData:{key:123456,key2:'vvvv'},
					fileSizeLimit:99999999999,
					showUploadedPercent:true,
					showUploadedSize:true,
					removeTimeout:9999999,
					uploader : '${basePath}/deveAmateurSchool/source_upload',
					onUploadStart:function(file){
						console.log(file.name+'开始上传');
					},
					onInit:function(obj){
						console.log('初始化');
						console.log(obj);
					},
					onUploadComplete:function(file,data){
						    debugger;
						    var fi = $.parseJSON(data);
						    //文件名称
						    var filename = fi.filename;
						    //生成后的文件名
						    var sysFilepath = fi.sysfilepath;
						    //文件路径
						    var filepath = fi.filepath;
						    file_path_one = filename + "," + filepath;
						    file_path = file_path+"|"+filepath;
						    up.filePath=file_path_one;
						    //保存到集合
			                fileMap[file.index]=new Array(file_path_one);
					},
					onCancel:function(file){
						console.log(file.name+'删除成功');
					},
					onClearQueue:function(queueItemCount){
						console.log('有'+queueItemCount+'个文件被删除了');
					},
					onDestroy:function(){
						console.log('destroyed!');
					},
					onSelect:function(file){
						console.log(file.name+'加入上传队列');
					},
					onQueueComplete:function(queueData){
						console.log('队列中的文件全部上传完成',queueData);
					}
			});	
		});
		
		//已有的数据进行删除操作
		function deleteByPK(id,filePath){
		    layer.confirm("您是否删除当前选中数据信息", {icon: 3, title:'提示',skin:'layer-ext-moon'}, function(index){
					$.ajax({
						type : "POST",
						url : "${basePath}/deveAmateurSchool/source_upload_update",
						//dataType:"json",
						data:{
							fileId : id,
							filePath : filePath
						},
						success : function(data) {
							if(data.success == "0000"){
								layer.alert('删除数据成功', {icon: 6});
								$("#"+id+"_hr").parent().parent().remove();
							}else{
								layer.alert('未写入数据库的文件没有删除！请联系管理员！', {icon: 5});
							}
						}
					})
			 },function(index){
			    layer.close(index);
		     });
	      }
		
		//删除图片
        function deletefile(id){
            var url = "${basePath}/partyInfomation/deletefile";
            $.ajax({
                type : "POST",
                url : url,
                dataType:"json",
                data:{
                    filepath : id
                },
                success : function(data) {
                    if(data.success == "0000"){
                        alert("删除数据成功！");
                        var file_name = id.split(",")[0];
                        $("#"+file_name+"_hr").parent().parent().remove();
                    }else{
                        alert("未写入数据库的文件没有删除！请联系管理员！");
                    }
                }
            })
        }
	</script>
</body>
</html>